package net.xi.dmhome.Utils;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * @Author: jxhu
 * @Description:加密算法
 * @Date Created in 2021-02-03 20:41
 * @Modified By:
 */
public class DesAesUtil {

    /* @Description: 加密算法,@Param:  */
    public static String desaes(String intput) throws Exception {
        String key = "hjx12345";
        //算法
        String transformation = "DES";
        //加密类型
        String algorithm = "Des";
        //创建加密对象
        Cipher cipher = Cipher.getInstance(transformation);
        //第一个参数：key的字节，第二个参数：加密的类型
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),transformation);
        //加密初始化
        cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
        //调用加密方法
        //参数表示原文字节数组
        byte[] bytes = cipher.doFinal(intput.getBytes());
        //对数据进行Base64编码
        String encode = Base64.encode(bytes);
        return encode;

    }

    /**
     * @Description: 解密算法
     * @Param:
     * @return:
     * @Author: jxhu
     * @Date: 2020/11/24
     */
    public static String decryptDES(String encryptDES)throws Exception{
        String key = "hjx12345";
        //算法
        String transformation = "DES";
        //加密类型
        String algorithm = "Des";

        Cipher cipher = Cipher.getInstance(transformation);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),algorithm);
        //解密
        cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
        byte[] bytes = cipher.doFinal(encryptDES.getBytes());
        //对数据进行Base64编码
        String encode = Base64.encode(bytes);
        return encode;

    }
}
